import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Login from '../view/Login.vue'
import Home from '../view/Home.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name:'Login',
    component: Login
  },
  {
    path: '/home',
    name:'Home',
    component: Home,
    redirect:'/users',
    children:[
      {
        path:'/users',
        name:'Users',
        component: ()=>import("../view/user/Users.vue")
      },
      {
        path:'/roles',
        name:'RoleList',
        component: ()=>import("../view/role/RoleList.vue")
      },
    ]
   
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to 将访问哪一个路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行
  //   next() 放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
